Vue.component('v-select',VueSelect.VueSelect)
let createQuestionApp = new Vue({
    el: '#createQuestionApp',
    data: {
        title:null,
        tags: [],
        selectedTagIds:[],
        teachers:[],
        selectedTeacherIds:[]
    },
    methods: {
        loadTags: function () {
            //alert("准备加载标签列表数据!")
            $.ajax({
                url: '/redis-tag/v1/tags',
                type: 'GET',
                dataType: 'json',
                success: function (json) {
                    //console.log(json.data);
                    for (var i =0;i<json.data.length;i++){
                        createQuestionApp.tags.push(
                            {label:json.data[i].name,value:json.data[i].id})
                    }
                }
            })
        },
        loadTeachers:function () {
            $.ajax({
                url:'/api/v1/users/teachers',
                type:'GET',
                dataType: 'json',
                success:function (json) {
                    for (var i =0;i<json.data.length;i++){
                        createQuestionApp.teachers.push(
                            {label:json.data[i].nickname,value:json.data[i].id})
                    }
                }
            })
        },
        postQuestion:function () {
            let title = createQuestionApp.title
            let selectedTagIds = createQuestionApp.selectedTagIds
            let selectedTeacherIds = createQuestionApp.selectedTeacherIds
            let content = $('#summernote').val()

            alert("准备提交数据，提交的数据可以在控制台查看")
            console.log(title)
            console.log(selectedTagIds)
            console.log(selectedTeacherIds)
            console.log(content)

            // 准备提交到服务器端的数据
            let data = {
                title:title,
                content:content,
                tagIds:selectedTagIds,
                teacherIds:selectedTeacherIds
            }
            // 发送请求，处理结束
            $.ajax({
                url:'/api/v1/questions/post',
                data:data,
                traditional:true,
                type:'POST',
                dataType:'json',
                success:function (json) {
                    if (json.state == 2000){
                        alert("发布问题成功!")
                    }else {
                        alert(json.message)
                    }
                }
            })
        }
    },
    created: function () {
        this.loadTags()
        this.loadTeachers()
    }
})
